package com.meeting.project.business.meeting.service.impl;

import java.util.List;

import com.meeting.common.utils.DateUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.meeting.common.utils.StringUtils;

import com.meeting.common.utils.security.ShiroUtils;
import com.meeting.framework.config.ServerConfig;
import com.meeting.project.business.meeting.domain.MeetingVO;
import com.meeting.project.business.record.domain.SignInRecord;
import com.meeting.project.business.meeting.mapper.MeetingMapper;
import com.meeting.project.business.meeting.domain.Meeting;
import com.meeting.project.business.meeting.service.IMeetingService;
import com.meeting.common.utils.text.Convert;
import com.meeting.project.business.record.mapper.SignInRecordMapper;

/**
 * 会议信息Service业务层处理
 *
 * @author llmaoa
 * @date 2025-08-05
 */
@Service
public class MeetingServiceImpl implements IMeetingService {
    @Autowired
    private MeetingMapper meetingMapper;

    @Autowired
    private SignInRecordMapper signInRecordMapper;

    @Autowired
    private ServerConfig serverConfig;

    private static final String QR_CODE_URL = "/app/record/qrcode/%s";

    /**
     * 查询会议信息
     *
     * @param id 会议信息主键
     * @return 会议信息
     */
    @Override
    public Meeting selectMeetingById(Long id) {
        return meetingMapper.selectMeetingById(id);
    }

    /**
     * 查询会议信息列表
     *
     * @param meeting 会议信息
     * @return 会议信息
     */
    @Override
    public List<Meeting> selectMeetingList(Meeting meeting) {
        return meetingMapper.selectMeetingList(meeting);
    }

    @Override
    public List<MeetingVO> selectMeetingVOList(Meeting meeting) {
        List<MeetingVO> meetingVOList =  meetingMapper.selectMeetingVOList(meeting);
        meetingVOList.forEach(item -> {
            if (item.getMeetingStatus() == 0) {
                item.setStatusStr("未开始");
            } else if (item.getMeetingStatus() == 1) {
                item.setStatusStr("进行中");
            } else if (item.getMeetingStatus() == 2) {
                item.setStatusStr("已结束");
            }
            item.setAbsentPeople(item.getMeetingPeople() - item.getSignPeople());
        });
        return meetingVOList;
    }

    /**
     * 新增会议信息
     *
     * @param meeting 会议信息
     * @return 结果
     */
    @Override
    public int insertMeeting(Meeting meeting) {
        meeting.setCreateBy(ShiroUtils.getLoginName());
        meeting.setCreateTime(DateUtils.getNowDate());
        int rows = meetingMapper.insertMeeting(meeting);

        // 更新二维码地址
        String url = serverConfig.getUrl() + String.format(QR_CODE_URL, meeting.getId());
        meeting.setMeetingImg(url);
        meetingMapper.updateMeeting(meeting);

        // 生产签到二维码 并更新到数据库
        /*String filePath = RuoYiConfig.getProfile();
        String format = "jpg";
        String fileName = "/meeting/signIn/" + meeting.getId() + "." + format;
        String url = serverConfig.getUrl() + "/app/record/" + meeting.getId();
        QRCodeGenerator.generateQRCode(url, 300, 300, filePath + fileName, format);
        meeting.setMeetingImg(Constants.RESOURCE_PREFIX + fileName);
        meetingMapper.updateMeeting(meeting);*/
        return rows;
    }

    /**
     * 修改会议信息
     *
     * @param meeting 会议信息
     * @return 结果
     */
    @Override
    public int updateMeeting(Meeting meeting) {
        meeting.setUpdateBy(ShiroUtils.getLoginName());
        meeting.setUpdateTime(DateUtils.getNowDate());
        return meetingMapper.updateMeeting(meeting);
    }

    /**
     * 批量删除会议信息
     *
     * @param ids 需要删除的会议信息主键
     * @return 结果
     */
    @Override
    public int deleteMeetingByIds(String ids) {
        return meetingMapper.deleteMeetingByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除会议信息信息
     *
     * @param id 会议信息主键
     * @return 结果
     */
    @Override
    public int deleteMeetingById(Long id) {
        return meetingMapper.deleteMeetingById(id);
    }

    @Override
    public MeetingVO getMeetingVOById(Long id) {
        return meetingMapper.getMeetingVOById(id);
    }

    /**
     * 新增人员签到信息信息
     *
     * @param meeting 会议信息对象
     */
    public void insertSignInRecord(Meeting meeting) {
        List<SignInRecord> signInRecordList = meeting.getSignInRecordList();
        Long id = meeting.getId();
        if (StringUtils.isNotNull(signInRecordList)) {
            List<SignInRecord> list = new ArrayList<SignInRecord>();
            for (SignInRecord signInRecord : signInRecordList) {
                signInRecord.setMeetingId(id);
                list.add(signInRecord);
            }
            if (list.size() > 0) {
                meetingMapper.batchSignInRecord(list);
            }
        }
    }
}
